package com.hubiao.pro.controller;

import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.DefaultTypedTuple;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashSet;
import java.util.Set;

/**
 * Description
 *
 * @author hubiao
 * @since 2021-01-05 22:24
 */
@RestController
@RequestMapping("/ranking")
public class RedisRankingListController {

    public static final String SCORE_RANK = "score_rank";
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @GetMapping("/batchAdd")
    public void batchAdd() {
        Set<ZSetOperations.TypedTuple<String>> tuples = new HashSet<>();
        long start = System.currentTimeMillis();
        for (int i = 0; i < 100000; i++) {
            DefaultTypedTuple<String> tuple = new DefaultTypedTuple<>( "张三" + i, 1D + i );
            tuples.add( tuple );
        }
        System.out.println( "循环时间:" + (System.currentTimeMillis() - start) );
        Long num = stringRedisTemplate.opsForZSet().add( SCORE_RANK, tuples );
        System.out.println( "批量新增时间:" + (System.currentTimeMillis() - start) );
        System.out.println( "受影响行数：" + num );
    }

    @GetMapping("/list")
    public void list() {
        Set<String> range = stringRedisTemplate.opsForZSet().reverseRange( SCORE_RANK, 0, 10 );
        System.out.println( "获取到的排行列表:" + JSON.toJSONString( range ) );
        Set<ZSetOperations.TypedTuple<String>> rangeWithScores = stringRedisTemplate.opsForZSet().reverseRangeWithScores( SCORE_RANK, 0, 10 );
        System.out.println( "获取到的排行和分数列表:" + JSON.toJSONString( rangeWithScores ) );
    }


    @GetMapping("/add")
    public void add() {
        stringRedisTemplate.opsForZSet().add( SCORE_RANK, "李四", 8899 );
    }


    @GetMapping("/find")
    public void find() {
        Long rankNum = stringRedisTemplate.opsForZSet().reverseRank( SCORE_RANK, "李四" );
        System.out.println( "李四的个人排名：" + rankNum );

        Double score = stringRedisTemplate.opsForZSet().score( SCORE_RANK, "李四" );
        System.out.println( "李四的分数:" + score );
    }


    @GetMapping("/count")
    public void count() {
        Long count = stringRedisTemplate.opsForZSet().count( SCORE_RANK, 8001, 9000 );
        System.out.println( "统计8001-9000之间的人数:" + count );
    }


    @GetMapping("/zCard")
    public void zCard() {
        Long aLong = stringRedisTemplate.opsForZSet().zCard( SCORE_RANK );
        System.out.println( "集合的基数为：" + aLong );
    }


    @GetMapping("/incrementScore")
    public void incrementScore() {
        Double score = stringRedisTemplate.opsForZSet().incrementScore( SCORE_RANK, "李四", 1000 );
        System.out.println( "李四分数+1000后：" + score );
    }
}
